package com.david.viewtest.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.widget.FrameLayout
import android.widget.TextView
import com.david.viewtest.R

//左右文本分开在两边的view
class StartEndTextItem(context: Context, attributeSet: AttributeSet):FrameLayout(context, attributeSet) {
    private var startTextView = TextView(context)
    private var endTextView = TextView(context)

    init {
        val startParam = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        startParam.gravity = Gravity.CENTER_VERTICAL
        addView(startTextView, startParam)

        val endParam = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        endParam.gravity = Gravity.CENTER_VERTICAL or Gravity.END
        addView(endTextView, endParam)


        val a = context.obtainStyledAttributes(attributeSet, R.styleable.startEndText)
        startTextView.setTextColor(a.getColor(R.styleable.startEndText_startEndTextStartColor, Color.BLACK))
        startTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, a.getDimension(R.styleable.startEndText_startEndTextStartSize, 12f))

        endTextView.setTextColor(a.getColor(R.styleable.startEndText_startEndTextEndColor, Color.BLACK))
        endTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, a.getDimension(R.styleable.startEndText_startEndTextStartSize, 12f))

        startTextView.text = a.getString(R.styleable.startEndText_startEndTextStartText)
        endTextView.text = a.getString(R.styleable.startEndText_startEndTextEndText)
        a.recycle()
    }

    fun setStartText(text: String?) {
        startTextView.text = text
    }

    fun setEndText(text: String?) {
        endTextView.text = text
    }
}